/**
 * v-hasPermi 操作权限处理
 * Copyright (c) 2019 ruoyi
 */

import store from "@/store";

function verifyPermission(el, binding) {
  const { value } = binding;
  const all_permission = "*:*:*";
  const permissions = store.getters && store.getters.permissions;

  let requiredPermissions = [];
  if (Array.isArray(value) && value.length > 0) {
    requiredPermissions = value;
  } else if (typeof value === "string" && value) {
    requiredPermissions = [value];
  } else {
    throw new Error(`请设置操作权限标签值`);
  }

  const hasPermissions = (permissions || []).some((permission) => {
    return (
      all_permission === permission || requiredPermissions.includes(permission)
    );
  });

  if (!hasPermissions) {
    el.parentNode && el.parentNode.removeChild(el);
  }
}

export default {
  inserted(el, binding) {
    verifyPermission(el, binding);
  },
  update(el, binding) {
    verifyPermission(el, binding);
  },
};
